package com.space.school.controller;

import com.space.school.domain.Order;
import com.space.school.domain.OrderMaster;
import com.space.school.domain.ProductInfo;
import com.space.school.services.OrderService;
import com.space.school.services.impl.OrderServiceImpl;
import com.space.school.utils.KeyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "orderServlet",value = "/orderServlet")
public class OrderServlet extends HttpServlet {
    OrderService orderService=new OrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        查询以上架商品信息
        req.setCharacterEncoding("utf-8");
        List<ProductInfo> productInfoList=orderService.productList();
        req.setAttribute("productInfoList",productInfoList);
        req.getRequestDispatcher("insertOrderDetail.jsp").forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String order_id=req.getParameter("order_id");
        String product_id=req.getParameter("product_id");
        String product_num=req.getParameter("product_num");
        String operate=req.getParameter("operate");
        if(order_id.equals("") || order_id==null){
            order_id= KeyUtil.generateKey();
        }
        if(operate.equals("add")){
//            继续添加
            String detail_id=KeyUtil.generateKey();
            Order order=new Order();
            order.setOrder_id(order_id);
            order.setDetail_id(detail_id);
            order.setProduct_num(Integer.parseInt(product_num));
            order.setProduct_id(Integer.parseInt(product_id));
            orderService.insert(order);
            req.setAttribute("order_id",order_id);
            List<ProductInfo> productInfoList=orderService.productList();
            req.setAttribute("productInfoList",productInfoList);
            req.getRequestDispatcher("insertOrderDetail.jsp").forward(req,resp);
        }else if(operate.equals("exit")){
//            退出操作
            orderService.delete(order_id);
            req.getRequestDispatcher("list.jsp").forward(req,resp);
        }else {
//            下一步操作
//           取总商品数量
//            先保存
            String detail_id=KeyUtil.generateKey();
            Order order=new Order();
            order.setOrder_id(order_id);
            order.setDetail_id(detail_id);
            order.setProduct_num(Integer.parseInt(product_num));
            order.setProduct_id(Integer.parseInt(product_id));
            orderService.insert(order);
            List<OrderMaster> orderMasters=orderService.totalList(order_id);
            req.setAttribute("orderMaster",orderMasters.get(0));
            req.setAttribute("order_id",order_id);
            req.getRequestDispatcher("insertOrderMaster.jsp").forward(req,resp);

        }

    }
}
